#include "dcsdboperator.h"

#include <QDebug>
#include <global.h>

DCSDbOperator::DCSDbOperator()
{
    mSqlopt = new SqliteOperator(DCS_DB_PATH);

    mSqlopt->openDb();

    if (!mSqlopt->isTableExist("masterParameters"))
    {
        qDebug() << "table masterParameters not exist, create";

        mSqlopt->createTable("masterParameters", "id integer primary key unique , \
                                                 slaveNum integer default 1 , \
                                                 slaveStartId integer default 1 , \
                                                 mainVideoSrc integer default 0, \
                                                 backupVideoSrc integer default 0 , \
                                                 fps integer default 25, \
                                                 mainNtpSrc integer default 0 , \
                                                 playMode integer default 0, \
                                                 backLightLvl integer default 3, \
                                                 turnoffBackLight integer default 5, \
                                                 interFace integer default 0");
    }

    if (!mSqlopt->isTableExist("playLists"))
    {
        mSqlopt->createTable("playLists", "id integer primary key unique, path char(128) not null");
    }

    if (!mSqlopt->isTableExist("slaveParameters"))
    {
        mSqlopt->createTable("slaveParameters", "id integer primary key unique, \
                             slaveId integer not null, \
                             chipType   integer not null, \
                             portChannelNum  integer not null, \
                             gravLvl integer not null , \
                             clockFreq integer not null , \
                             clockFreqRead integer not null , \
                             ch0FineTune integer not null , \
                             dutyRate integer not null , \
                             grayFilte integer not null , \
                             colorNum integer not null, \
                             oe integer not null, \
                             gammaTuneType integer not null, \
                             gamma1 integer not null, \
                             gamma2 integer not null, \
                             gamma3 integer not null, \
                             gamma4 integer not null, \
                             brightnessTuneType integer not null, \
                             brightness1 integer not null, \
                             brightness2 integer not null, \
                             brightness3 integer not null, \
                             brightness4 integer not null, \
                             refleshType integer not null, \
                             refleshValue integer not null, \
                             currentGain1 integer not null, \
                             currentGain2 integer not null, \
                             currentGain3 integer not null, \
                             currentGain4 integer not null, \
                             outputInverse integer not null , \
                             addrPinDefault  integer not null, \
                             hold integer not null");
    }
}

DCSDbOperator::~DCSDbOperator()
{
//    close();
    delete mSqlopt;
}

/**
 * @brief DCSDbOperator::setMasterParameterSlaveNum
 *          设置分控数
 * @param slaveNum
 * @return
 */
bool DCSDbOperator::setMasterParameterSlaveNum(quint8 slaveNum)
{
    QSqlQuery sqlQuery;

    if (sqlQuery.prepare("REPLACE INTO masterParameters (id,slaveNum) VALUES(:id, :slaveNum)"))
    {
        sqlQuery.bindValue(":id", 1);
        sqlQuery.bindValue(":slaveNum", slaveNum);
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to replace data. " << sqlQuery.lastError();
            return false;
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        return false;
    }

    return true;
}

/**
 * @brief DCSDbOperator::getMasterParameterSlaveNum
 *          获取分控数
 * @return
 */
bool DCSDbOperator::getMasterParameterSlaveNum(quint8 &slaveNum)
{
    QSqlQuery sqlQuery;
    QString selectSql = QString("SELECT slaveNum FROM masterParameters WHERE id==1");

    if (sqlQuery.prepare(selectSql))
    {
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
            return false;
        }
        else
        {
            if (sqlQuery.next())
            {
                slaveNum = sqlQuery.value(0).toInt();
                qDebug() << "slaveNum:" << slaveNum;
            }
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        qDebug() << "Error: Fail to prepare, slq:" << selectSql;
        return false;
    }

    return true;
}

bool DCSDbOperator::setMasterParameterFPS(quint8 fps)
{
    QSqlQuery sqlQuery;

    if (sqlQuery.prepare("REPLACE INTO masterParameters (id,fps) VALUES(:id, :fps)"))
    {
        sqlQuery.bindValue(":id", 1);
        sqlQuery.bindValue(":fps", fps);
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to replace data. " << sqlQuery.lastError();
            return false;
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        return false;
    }

    return true;
}

bool DCSDbOperator::getMasterParameterFPS(quint8 &fps)
{
    QSqlQuery sqlQuery;
    QString selectSql = QString("SELECT fps FROM masterParameters WHERE id==1");

    if (sqlQuery.prepare(selectSql))
    {
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
            return false;
        }
        else
        {
            if (sqlQuery.next())
            {
                fps = sqlQuery.value(0).toInt();
                qDebug() << "fps:" << fps;
            }
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        qDebug() << "Error: Fail to prepare, slq:" << selectSql;
        return false;
    }

    return true;
}

bool DCSDbOperator::setMasterParameterPlayMode(quint8 mode)
{
    QSqlQuery sqlQuery;

    if (sqlQuery.prepare("REPLACE INTO masterParameters (id,playMode) VALUES(:id, :playMode)"))
    {
        sqlQuery.bindValue(":id", 1);
        sqlQuery.bindValue(":playMode", mode);
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to replace data. " << sqlQuery.lastError();
            return false;
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        return false;
    }

    return true;
}

bool DCSDbOperator::getMasterParameterPlayMode(quint8 &mode)
{
    QSqlQuery sqlQuery;
    QString selectSql = QString("SELECT playMode FROM masterParameters WHERE id==1");

    if (sqlQuery.prepare(selectSql))
    {
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
            return false;
        }
        else
        {
            if (sqlQuery.next())
            {
                mode = sqlQuery.value(0).toInt();
                qDebug() << "playMode:" << mode;
            }
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        qDebug() << "Error: Fail to prepare, slq:" << selectSql;
        return false;
    }

    return true;
}

bool DCSDbOperator::setMasterParameter(struct masterParam &para)
{
    QSqlQuery sqlQuery;

    if (sqlQuery.prepare("REPLACE INTO masterParameters (id, slaveNum, slaveStartId, mainVideoSrc, \
                          backupVideoSrc, fps, mainNtpSrc, playMode, backLightLvl, turnoffBackLight) \
                          VALUES(:id, :slaveNum, :startSlaveId, :mainVideoSrc, \
                                 :backupVideoSrc, :fps, :mainNtpSrc, :playMode, :backLightLvl, :turnoffBackLight)"))
    {
        sqlQuery.bindValue(":id", 1);
        sqlQuery.bindValue(":slaveNum", para.slaveNum);
        sqlQuery.bindValue(":slaveStartId", para.slaveStartId);
        sqlQuery.bindValue(":mainVideoSrc", para.mainVideoSrc);
        sqlQuery.bindValue(":backupVideoSrc", para.backupVideoSrc);
        sqlQuery.bindValue(":fps", para.fps);
        sqlQuery.bindValue(":mainNtpSrc", para.mainNtpSrc);
        sqlQuery.bindValue(":playMode", para.playMode);
        sqlQuery.bindValue(":backLightLvl", para.backLightLvl);
        sqlQuery.bindValue(":turnoffBackLight", para.turnoffBackLight);
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to replace data. " << sqlQuery.lastError();
            return false;
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        return false;
    }

    return true;
}

bool DCSDbOperator::getMasterParameter(struct masterParam &para)
{
    QSqlQuery sqlQuery;
    QString selectSql = QString("SELECT * FROM masterParameters WHERE id==1");

    if (sqlQuery.prepare(selectSql))
    {
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
            return false;
        }
        else
        {
            if (sqlQuery.next())
            {
                para.id = sqlQuery.value(0).toUInt();
                para.slaveNum = sqlQuery.value(1).toUInt();
                para.slaveStartId = sqlQuery.value(2).toUInt();
                para.mainVideoSrc = sqlQuery.value(3).toUInt();
                para.backupVideoSrc = sqlQuery.value(4).toUInt();
                para.fps = sqlQuery.value(5).toUInt();
                para.mainNtpSrc = sqlQuery.value(6).toUInt();
                para.playMode = sqlQuery.value(7).toUInt();
                para.backLightLvl = sqlQuery.value(8).toUInt();
                para.turnoffBackLight = sqlQuery.value(9).toUInt();
            }
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        qDebug() << "Error: Fail to prepare, slq:" << selectSql;
        return false;
    }

    return true;
}

bool DCSDbOperator::setMasterParameterMainVideoSrc(quint8 src)
{
    QSqlQuery sqlQuery;

    if (sqlQuery.prepare("REPLACE INTO masterParameters (id,mainVideoSrc) VALUES(:id, :mainVideoSrc)"))
    {
        sqlQuery.bindValue(":id", 1);
        sqlQuery.bindValue(":mainVideoSrc", src);
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to replace data. " << sqlQuery.lastError();
            return false;
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        return false;
    }

    return true;
}

bool DCSDbOperator::getMasterParameterMainVideoSrc(quint8 &src)
{
    QSqlQuery sqlQuery;
    QString selectSql = QString("SELECT mainVideoSrc FROM masterParameters WHERE id==1");

    if (sqlQuery.prepare(selectSql))
    {
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
            return false;
        }
        else
        {
            if (sqlQuery.next())
            {
                src = sqlQuery.value(0).toInt();
                qDebug() << "mainVideoSrc:" << src;
            }
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        qDebug() << "Error: Fail to prepare, slq:" << selectSql;
        return false;
    }

    return true;
}

bool DCSDbOperator::setMasterParameterBackupVideoSrc(quint8 src)
{
    QSqlQuery sqlQuery;

    if (sqlQuery.prepare("REPLACE INTO masterParameters (id,backupVideoSrc) VALUES(:id, :backupVideoSrc)"))
    {
        sqlQuery.bindValue(":id", 1);
        sqlQuery.bindValue(":backupVideoSrc", src);
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to replace data. " << sqlQuery.lastError();
            return false;
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        return false;
    }

    return true;
}

bool DCSDbOperator::getMasterParameterBackupVideoSrc(quint8 &src)
{
    QSqlQuery sqlQuery;
    QString selectSql = QString("SELECT backupVideoSrc FROM masterParameters WHERE id==1");

    if (sqlQuery.prepare(selectSql))
    {
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
            return false;
        }
        else
        {
            if (sqlQuery.next())
            {
                src = sqlQuery.value(0).toInt();
                qDebug() << "backupVideoSrc:" << src;
            }
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        qDebug() << "Error: Fail to prepare, slq:" << selectSql;
        return false;
    }

    return true;
}

bool DCSDbOperator::setMasterParameterInterface(quint8 iface)
{
    QSqlQuery sqlQuery;

    if (sqlQuery.prepare("REPLACE INTO masterParameters (id,interFace) VALUES(:id, :interFace)"))
    {
        sqlQuery.bindValue(":id", 1);
        sqlQuery.bindValue(":interFace", iface);
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to replace data. " << sqlQuery.lastError();
            return false;
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        return false;
    }

    return true;
}

bool DCSDbOperator::getMasterParameterInterface(quint8 &iface)
{
    QSqlQuery sqlQuery;
    QString selectSql = QString("SELECT interFace FROM masterParameters WHERE id==1");

    if (sqlQuery.prepare(selectSql))
    {
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
            return false;
        }
        else
        {
            if (sqlQuery.next())
            {
                iface = sqlQuery.value(0).toInt();
                qDebug() << "interface:" << iface;
            }
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        qDebug() << "Error: Fail to prepare, slq:" << selectSql;
        return false;
    }

    return true;
}

bool DCSDbOperator::clearPlayLists()
{
    return mSqlopt->cleanTable("playLists");
}

bool DCSDbOperator::addPlayItem(playListsItem &item)
{
    QSqlQuery sqlQuery;

    if (sqlQuery.prepare("INSERT INTO playLists (path) VALUES(:path)"))
    {
        sqlQuery.bindValue(":path", item.path);
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to replace data. " << sqlQuery.lastError();
            return false;
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        return false;
    }

    return true;
}

bool DCSDbOperator::insertPlayItem(quint8 index, playListsItem &item)
{
    QSqlQuery sqlQuery;


    if (sqlQuery.prepare("INSERT INTO playLists (id,path) VALUES(:id, :path) )"))
    {
        sqlQuery.bindValue(":id", index);
        sqlQuery.bindValue(":path", item.path);
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to replace data. " << sqlQuery.lastError();
            return false;
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        return false;
    }

    return true;
}

bool DCSDbOperator::getPlayLists(QList<QString> &lists)
{
    QSqlQuery sqlQuery;
    QString selectSql = QString("SELECT path FROM playLists");

    if (sqlQuery.prepare(selectSql))
    {
        if (!sqlQuery.exec())
        {
            qDebug() << "Error: Fail to query table. " << sqlQuery.lastError();
            return false;
        }
        else
        {
            while (sqlQuery.next())
            {
                qDebug() << "play list item:" << sqlQuery.value(0).toString();
                lists.append(sqlQuery.value(0).toString());
            }
        }
    }
    else
    {
        qDebug() << "Error:" << sqlQuery.lastError();
        qDebug() << "Error: Fail to prepare, slq:" << selectSql;
        return false;
    }

    return true;
}

void DCSDbOperator::open()
{
    mSqlopt->openDb();
}


void DCSDbOperator::close()
{
    mSqlopt->closeDb();
}
